<?php
class CapacityWebSolutions_ImportProduct_Model_Convert_Adapter_Customer extends Mage_Customer_Model_Convert_Adapter_Customer{


    public function saveAttribute($importData){

        $importdata['attribute'] = strtolower($importData['attribute']);
        if (empty($importData['attribute'])) {
                $message = Mage::helper('catalog')->__('Skip import row, is  empty value "%s" ', 'attribute');
                Mage::throwException($message);
        }
        $setup = new Mage_Eav_Model_Entity_Setup('core_setup'); 
        $setup->addAttribute('customer', $importData['attribute'], array(
            'label' => $importData['label'],
            'type' => $importData['type'],//'varchar',
            'input' =>$importData['input'],// 'text',
            'visible' => $importData['visible'],//true,
            'required' => $importData['required'],//true,
            'position' =>$importData['position']
        ));
        //find out the attribute_id of the newly created attribute
        $sql = "SELECT attribute_id FROM ".$setup->getTable('eav_attribute')." WHERE attribute_code='".$importData['attribute']."'";
        $row = Mage::getSingleton('core/resource')->getConnection('core_read')->fetchRow($sql);

        //insert the supporting values into customer_form_attribute
        $setup->run("
            INSERT INTO customer_form_attribute (form_code, attribute_id)
            VALUES (
                'adminhtml_customer',".$row['attribute_id']."
            );
        ");

      /*  $setup->run("
            INSERT INTO customer_form_attribute (form_code, attribute_id)
            VALUES (
                'checkout_register', ".$row['attribute_id']."
            );
        ");

        $setup->run("
            INSERT INTO customer_form_attribute (form_code, attribute_id)
            VALUES (
                'customer_account_create', ".$row['attribute_id']."
            );
        ");

        $setup->run("
            INSERT INTO customer_form_attribute (form_code, attribute_id)
            VALUES (
                'customer_account_edit', ".$row['attribute_id']."
            );
        ");*/

        return true;
    }
    public function saveRow($importData)
    {

        if(isset($importData['attribute'])){
            return $this->saveAttribute($importData); 
        }


        if(isset($importData['Login'])){

           
            $row = array();
            foreach($importData as $key=>$value){
                  $row[trim($key)] =  $value;
            }
            unset($importData);
            $importData = array(); 
            $importData['email'] =  $row['E-mail'];
            $importData['customerid'] = $row['member id'];
            $importData['increment_id'] = $row['member id'];
            $importData['created_at'] = date('Y-m-d h:i:s',$row['created at']);
            $groud = array('0'=>'General','1'=>'Wholesale','28'=>'Platinium','29'=>'Silver','30'=>'Gold','32'=>'Bronze');
            $importData['group_id'] =  $importData['group'] = $groud[$row['Membership ID']];  
            $importData['password_hash'] =  $row['Password'];  
            $title = array('mr'=>'Male','mrs'=>'Female','ms'=>'Female');
            $importData['gender'] =  $title[trim($row['Title'])];  
            $importData['firstname'] =  $row['First name'];  
            $importData['lastname'] =  $row['Last name'];  
            $importData['shipping_company'] = $importData['billing_company'] =  $row['Company'];  
            $importData['shipping_fax'] = $importData['billing_fax'] =  $row['Fax'];  
            $importData['shipping_telephone'] = $importData['billing_telephone'] =  $row['Phone'];  
            $importData['billing_prefix'] =  $row['Billing: title'];  
            $importData['billing_firstname'] =  $row['Billing: first name'];  
            $importData['billing_lastname'] =  $row['Billing: last name'];  
            $importData['billing_street_full'] =  $row['Billing: address'];  
            $importData['billing_street_full'] .=  "\r\n".$row['Billing: address (line 2)'];  
            $importData['billing_city'] =  $row['Billing: city'];  
            $importData['billing_region'] =  $row['Billing: state'];  
            $importData['billing_country'] =  $row['Billing: country'];  
            $importData['billing_postcode'] =  $row['Billing: zipcode'];  
            $importData['shipping_prefix'] =  $row['Shipping: title'];  
            $importData['shipping_firstname'] =  $row['Shipping: first name'];  
            $importData['shipping_lastname'] =  $row['Shipping: last name'];  
            $importData['shipping_street_full'] =  $row['Shipping: address'];  
            $importData['shipping_street_full'] .= "\r\n".$row['Shipping: address (line 2)'];  
            $importData['shipping_city'] =  $row['Shipping: city'];  
            $importData['shipping_region'] =  $row['Shipping: state'];  
            $importData['shipping_country'] =  $row['Shipping: country'];  
            $importData['shipping_postcode'] =  $row['Shipping: zipcode']; 
            $importData['created_in'] =  'default';  $importData['is_subscribed'] =  '0';  $importData['website'] =  'base';  
           
            if(empty($importData['billing_country'])) $importData['billing_country'] = $importData['shipping_country'];
            unset($row);
            //Mage::log($importData);
            $return  = parent::saveRow($importData);
            $customer = $this->getCustomerModel();
            $customer->setIncrementId( $importData['increment_id']);
              
            $customer->save();
            return $return;
        } 
        return parent::saveRow($importData);
    }
}
